package com.sskj.hangqing.ui.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.base.App;
import com.sskj.common.box.imageloader.GlideRoundImageLoader;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.hangqing.R;
import com.sskj.hangqing.R2;
import com.sskj.hangqing.bean.HangqingBannerBean;
import com.sskj.hangqing.bean.NoticeBean;
import com.sskj.hangqing.bean.ZixunListBean;
import com.sskj.hangqing.component.DaggerUserDataComponent;
import com.sskj.hangqing.http.HttpConfig;
import com.sskj.hangqing.presenter.HangqingFragmentPresenter;
import com.sskj.hangqing.ui.activity.GuideWebActivity;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

@SuppressWarnings("ALL")
@Route(path = RConfig.HANG_HANGQING)
public class HangqingFragment extends BaseFragment<HangqingFragmentPresenter> {

    @BindView(R2.id.banner)
    Banner banner;
    @BindView(R2.id.tvNotice)
    TextSwitcher tvNotice;
    @BindView(R2.id.tvMoreNotice)
    TextView tvMoreNotice;
    @BindView(R2.id.llHelp)
    LinearLayout llHelp;
    @BindView(R2.id.llPublish)
    LinearLayout llPublish;
    private Disposable noticeDispo;
    private NoticeBean noticeList;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private Disposable bannerDispo;
    private SlimAdapter<ZixunListBean> slimAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.hang_fragment_hangqing;
    }

    @Override
    public HangqingFragmentPresenter getPresenter() {
        return new HangqingFragmentPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        ClickUtil.click(llHelp, () -> {
            ARouter.getInstance().build(RConfig.MINE_HELP_CENTER).navigation();
        });

        ClickUtil.click(llPublish, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            if (!AppCircleCheckUtil.checkHighAuth(getActivity(), userData)) {  // 高级实名
                return;
            }
            if (!AppCircleCheckUtil.checkShop(getActivity(), userData)) {  //是否是商家
                return;
            }
            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {  // 资金密码
                return;
            }
            ARouter.getInstance().build(RConfig.FABI_PUBLISH)
                    .withBoolean(Constans.IS_BUY, true)
                    .navigation();
        });

        ClickUtil.click(tvMoreNotice, () -> ARouter.getInstance().build(RConfig.HANG_GONGGAO_LIST).navigation());
        banner.setImageLoader(new GlideRoundImageLoader());
        tvNotice.setFactory(() -> {
            TextView textView = new TextView(getActivity());
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13);
            textView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
            textView.setGravity(Gravity.CENTER_VERTICAL);
            textView.setSingleLine();
            textView.setEllipsize(TextUtils.TruncateAt.END);
            textView.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.hangTextContent));
            return textView;
        });

        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.frameLayout, (Fragment) ARouter.getInstance().build(RConfig.HANG_FRAGMENT_COIN_LIST).navigation())
                .commitAllowingStateLoss();

    }

    @Override
    public void initData() {

        mPresenter.getBanner();
        mPresenter.getNotice();

    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }

    public void updateBanner(List<HangqingBannerBean> data) {

        if (banner == null)
            return;

        ArrayList<String> strings = new ArrayList<>();
        for (HangqingBannerBean bannerItem : data) {
            strings.add(HttpConfig.BASE_IMG_URL + bannerItem.getImgUrl());
        }

        banner.setImages(strings);
        banner.setImageLoader(new GlideRoundImageLoader());
        banner.setOffscreenPageLimit(3);
        banner.setDelayTime(2000);
        banner.start();
    }

    @Override
    public void onDestroy() {
        DisposUtil.close(noticeDispo);
        DisposUtil.close(bannerDispo);
        super.onDestroy();
    }


    public void updateNotice(List<NoticeBean> notice) {

        if (tvNotice != null) {
            if (notice.size() > 0) {
                DisposUtil.close(noticeDispo);
                noticeDispo = Flowable.interval(0, 5, TimeUnit.SECONDS)
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(i -> {
                            if (tvNotice != null) {
                                noticeList = notice.get((int) (i % notice.size()));
                                tvNotice.setText(noticeList.getTitle());
                                ClickUtil.click(tvNotice, () -> {
                                    Intent intent = new Intent(getContext(), GuideWebActivity.class);
                                    intent.putExtra(Constans.IS_HANGQING_NOTICE, true);
                                    intent.putExtra(Constans.HEAD, App.INSTANCE.getString(R.string.hangqinggonggaoListActivity2));
                                    intent.putExtra(Constans.TITLE, noticeList.getTitle());
                                    intent.putExtra(Constans.CONTENT, noticeList.getDetails());
                                    intent.putExtra(Constans.TIME, noticeList.getCreateTime());
                                    startActivity(intent);
                                });
                            }
                        }, System.out::println);
            }
        }
    }

}
